package zcrypto

import (
	"fmt"
	"gitee.com/zouyang8210/zjson"
	"sort"
)

type SignatureInfo struct {
	Sign    string // 签名
	WaitStr string // 待签名字符串
}

// Signature 签名
func Signature(v interface{}, apiSecret, signFieldName string) (s SignatureInfo, e error) {
	m := make(map[string]interface{})
	e = zjson.ObjToObj(&m, v)
	if e != nil {
		return
	}
	var keys []string
	var waitSignStr string
	for k, _ := range m {
		if k == signFieldName || k == "" {
			continue
		}
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, va := range keys {
		waitSignStr += fmt.Sprintf("%s=%v&", va, m[va])
	}
	s.Sign = Md5(s.WaitStr + apiSecret)
	return
}
